package org.chen.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.chen.domain.entity.SystemConfig;
import org.chen.domain.vo.SystemConfigVO;
import org.chen.mapper.SystemConfigMapper;
import org.chen.service.SystemConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class SystemConfigServiceImpl extends ServiceImpl<SystemConfigMapper, SystemConfig> implements SystemConfigService {
    @Autowired
    private SystemConfigMapper systemConfigMapper;

    public SystemConfigVO getSystemInfo() {
        SystemConfig config = systemConfigMapper.getConfig();
        SystemConfigVO vo = new SystemConfigVO();
        if (config != null) {
            vo.setSystemName(config.getSystemName());
        }
        return vo;
    }

    @Transactional(rollbackFor = Exception.class)
    public boolean updateSystemInfo(SystemConfigVO vo) {
        SystemConfig config = systemConfigMapper.getConfig();
        if (config == null) {
            config = new SystemConfig();
            config.setSystemName(vo.getSystemName());
            return systemConfigMapper.insert(config) > 0;
        }
        return systemConfigMapper.updateSystemName(config.getId(), vo.getSystemName()) > 0;
    }
}
